package com.fw.ui.view.common;

import org.vaadin.dialogs.ConfirmDialog;

import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class ConfirmDialogWindow {

    public static void createConfirmDialog(UI ui, String title, String content, String yesCaption, String noCaption,
	    ConfirmDialog.Listener listener, String height, String width) {

	try {
	    ConfirmDialog d = ConfirmDialog.show(ui, title, content, yesCaption, noCaption, listener);
	    Component c = d.getContent();
	    VerticalLayout verticalLayout = (VerticalLayout) c;
	    Panel panel = (Panel) verticalLayout.getComponent(0);
	    HorizontalLayout horizontalLayout = (HorizontalLayout) verticalLayout.getComponent(1);
	    if (content != null && content.trim().length() > 0) {
		VerticalLayout contentVerticalLayout = new VerticalLayout();
		Label label = new Label();
		label.setContentMode(ContentMode.HTML);
		label.setValue(content);
		contentVerticalLayout.addComponent(label);
		panel.setContent(contentVerticalLayout);
	    }
	    if (horizontalLayout != null && horizontalLayout.getComponentCount() > 0) {
		Button btnCancel = (Button) horizontalLayout.getComponent(0);
		Button btnOk = (Button) horizontalLayout.getComponent(1);
		btnOk.setWidth("70px");
		btnCancel.setWidth("70px");
		btnOk.setStyleName("button-blue");
		btnCancel.setStyleName("button-red");
	    }
	    d.setHeight(height);
	    d.setWidth(width);
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

    public static void createConfirmDialog(UI ui, String title, String content, String okCaption, ConfirmDialog.Listener listener,
	    String height, String width) {

	try {
	    ConfirmDialog d = ConfirmDialog.show(ui, title, content, okCaption, null, listener);
	    Component c = d.getContent();
	    VerticalLayout verticalLayout = (VerticalLayout) c;
	    Panel panel = (Panel) verticalLayout.getComponent(0);
	    HorizontalLayout horizontalLayout = (HorizontalLayout) verticalLayout.getComponent(1);
	    if (content != null && content.trim().length() > 0) {
		VerticalLayout contentVerticalLayout = new VerticalLayout();
		Label label = new Label();
		label.setContentMode(ContentMode.HTML);
		label.setValue(content);
		contentVerticalLayout.addComponent(label);
		panel.setContent(contentVerticalLayout);
	    }
	    if (horizontalLayout != null && horizontalLayout.getComponentCount() > 0) {
		Button btnCancel = (Button) horizontalLayout.getComponent(0);
		Button btnOk = (Button) horizontalLayout.getComponent(1);
		horizontalLayout.removeComponent(btnCancel);
		btnOk.setWidth("70px");
		btnOk.setStyleName("button-blue");
	    }
	    d.setHeight(height);
	    d.setWidth(width);
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

    public static void createConfirmDialog(UI ui, String title, TablePanel tablePanel, String yesCaption, String noCaption,
	    ConfirmDialog.Listener listener, String height, String width) {

	try {
	    ConfirmDialog d = ConfirmDialog.show(ui, title, "", yesCaption, noCaption, listener);
	    Component c = d.getContent();
	    VerticalLayout verticalLayout = (VerticalLayout) c;
	    Panel panel = (Panel) verticalLayout.getComponent(0);
	    HorizontalLayout horizontalLayout = (HorizontalLayout) verticalLayout.getComponent(1);
	    if (tablePanel != null) {
		panel.setContent(tablePanel);
	    }
	    if (horizontalLayout != null && horizontalLayout.getComponentCount() > 0) {
		Button btnCancel = (Button) horizontalLayout.getComponent(0);
		Button btnOk = (Button) horizontalLayout.getComponent(1);
		btnOk.setWidth("70px");
		btnCancel.setWidth("70px");
		btnOk.setStyleName("button-blue");
		btnCancel.setStyleName("button-red");
	    }
	    d.setWidth(width);
	    d.setHeight(height);
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

    public static void createConfirmDialog(UI ui, String title, Component component, String yesCaption, String noCaption,
	    ConfirmDialog.Listener listener, String height, String width) {

	try {
	    ConfirmDialog d = ConfirmDialog.show(ui, title, "", yesCaption, noCaption, listener);
	    Component c = d.getContent();
	    VerticalLayout verticalLayout = (VerticalLayout) c;
	    Panel panel = (Panel) verticalLayout.getComponent(0);
	    HorizontalLayout horizontalLayout = (HorizontalLayout) verticalLayout.getComponent(1);
	    if (component != null) {
		panel.setContent(component);
	    }
	    if (horizontalLayout != null && horizontalLayout.getComponentCount() > 0) {
		Button btnCancel = (Button) horizontalLayout.getComponent(0);
		Button btnOk = (Button) horizontalLayout.getComponent(1);
		btnOk.setWidth("70px");
		btnCancel.setWidth("70px");
		btnOk.setStyleName("button-blue");
		btnCancel.setStyleName("button-red");
	    }
	    d.setWidth(width);
	    d.setHeight(height);
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

}
